!function() {
    getApp();
    var t = new (require("../../utils/waterMark.js"))(), a = {
        colorMap: [ [ "0", "#ffffff" ], [ "1", "#979797" ], [ "2", "#000000" ], [ "3", "#ee0000" ], [ "4", "#f08600" ], [ "5", "#035aff" ], [ "6", "#00FF00" ] ],
        waterMarkConfig: {
            color: "#ffffff",
            opacity: .5
        },
        text: "请输入文字",
        debounceDelay: 200
    }, e = getApp();
    Page({
        data: {
            app: "",
            bool: !1,
            canvasW: 100,
            canvasH: 200,
            inputFocus: !1,
            currentColorIndex: -1,
            ad: {}
        },
        showMask: function() {
            this.setData({
                hasMask: !0,
                bool: !0
            });
        },
        hideMask: function() {
            this.setData({
                hasMask: !1,
                bool: !1
            });
        },
        onShow(){
            this.getRandomAd()
            getApp().chaping()
        },
        onLoad: function() {
            getApp().getRandomAd(true,this)
            this.getRandomAd(true)
        },
        test: function(t) {
            var a = t.currentTarget.dataset;
            wx.navigateToMiniProgram({
                appId: a.id,
                path: "",
                extraData: {
                    foo: "bar"
                },
                envVersion: "release",
                success: function(t) {
                    console.log(t);
                }
            });
        },
        onReady: function() {
            this.getDefaultConfig();
        },
        touchImgstart: function(t) {
            this.setData({
                touch: t.changedTouches[0],
                touchStartTime: new Date()
            });
        },
        touchImgend: function(t) {
            var a = Math.abs, e = this.data.touch, n = t.changedTouches[0], o = this.data.touchStartTime;
            350 <= new Date() - o || 10 > a(e.clientX - n.clientX) && 10 > a(e.clientY - n.clientY) && this.chooseImg();
        },
        chooseImg: function() {
            var e = this;
            wx.chooseImage({
                count: 1,
                success: async function(n) {
                    let imgData = await getApp().checkImg(n.tempFilePaths[0],e)
                    console.log(imgData)
                    if(!imgData||imgData.result.errCode!==0){
                        wx.showModal({
                        confirmText: '确定',
                        content: '图片内容不合法，请重新选择',
                        showCancel: false
                        })
                        return
                    }
                    wx.getImageInfo({
                        src: n.tempFilePaths[0],
                        success: function(o) {
                            var i = o.width, s = o.height, c = wx.getSystemInfoSync().windowWidth, r = c * s / i;
                            e.setData({
                                canvasW: c,
                                canvasH: r,
                                imgW: i,
                                imgH: s
                            });
                            var u = {
                                text: e.data.text,
                                id: "myCanvas",
                                color: "#ffffff",
                                xStart: 0,
                                yStart: -.71 * o.width,
                                xSpace: 60,
                                ySpace: 60,
                                rotate: 45,
                                opacity: .5,
                                width: c,
                                height: r,
                                scale: c / i,
                                size: 30,
                                imgUrl: n.tempFilePaths[0]
                            };
                            u = Object.assign(u, a.waterMarkConfig), t.mark(u).then(function() {
                                e.log(1), e.setData({
                                    draw: !0
                                }), -1 === e.data.currentColorIndex && -1 !== e.data.defaultColorIndex && e.setData({
                                    currentColorIndex: e.data.defaultColorIndex
                                });
                            });
                        }
                    });
                }
            });
        },
        saveImg: function() {
            return !!this.data.draw && (wx.showLoading({
                title: "保存中"
            }), wx.canvasToTempFilePath({
                canvasId: "myCanvas",
                destWidth: this.data.imgW,
                destHeight: this.data.imgH,
                success: function(t) {
                    wx.saveImageToPhotosAlbum({
                        filePath: t.tempFilePath,
                        success: function() {
                            wx.hideLoading(), wx.showToast({
                                title: "已保存到相册",
                                icon: "success",
                                duration: 2e3
                            });
                        },
                        fail(e){
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: '需要您授权保存相册',
                                showCancel: false,
                                success: modalSuccess => {
                                  wx.openSetting({
                                    success(settingdata) {
                                      console.log("settingdata", settingdata)
                                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                        wx.showModal({
                                          title: '提示',
                                          content: '获取权限成功,再次点击即可保存。',
                                          showCancel: false,
                                        })
                                      } else {
                                        wx.showModal({
                                          title: '提示',
                                          content: '获取权限失败，将无法保存到相册哦~',
                                          showCancel: false,
                                        })
                                      }
                                    },
                                    fail(failData) {
                                      console.log("failData", failData)
                                    },
                                    complete(finishData) {
                                      console.log("finishData", finishData)
                                    }
                                  })
                                }
                            })
                        }
                    });
                }
            }), void this.log(3));
        },
        async previewImg () {
            this.getRandomAd()
            let checkdata = await getApp().checkMsg(this.data.text)
            console.log(this.data.text)
            console.log(checkdata)
            if(!checkdata||checkdata.result.errCode!==0){
                wx.showModal({
                confirmText: '确定',
                content: '输入内容含有敏感词，请重新输入',
                showCancel: false
                })
                return
            }
            return !!this.data.draw && (wx.showLoading({
                title: "加载中"
            }), void wx.canvasToTempFilePath({
                canvasId: "myCanvas",
                destWidth: this.data.imgW,
                destHeight: this.data.imgH,
                success: function(t) {
                    wx.hideLoading(), wx.navigateTo({
                        url: "/pages/preview/preview?imageurl=" + t.tempFilePath
                    });
                }
            }));
        },
        handleCircleClick: function(e) {
            this.getRandomAd()
            var n = e.target.id;
            if (n) {
                var o = this;
                this.setData({
                    currentColorIndex: n
                }, function() {
                    return a.waterMarkConfig.color = o.data.ColorMap[o.data.currentColorIndex][1], !!o.data.draw && (t.reRendering({
                        color: a.waterMarkConfig.color
                    }), void o.log(1));
                });
            }
        },
        handleTextChange: function(t, a) {
            console.log(t,a)
            var e, n, o, i, s, c = function c() {
                var r = new Date().getTime() - i;
                r < a && 0 <= r ? e = setTimeout(c, a - r) : (e = null, s = t.apply(o, n), e || (o = n = null));
            };
            return function() {
                return o = this, n = arguments, i = new Date().getTime(), e || (e = setTimeout(c, a)), 
                s;
            };
        }(function(a) {
            this.data.draw && t.reRendering({
                text: a.detail.value
            });
        }, a.debounceDelay),
        handleTextFocus: function() {
            this.setData({
                inputFocus: !0
            });
        },
        handleTextBlur: function(t) {
            var a = this.data.text, e = this;
            this.setData({
                inputFocus: !1,
                text: t.detail.value
            }, function() {
                e.data.draw && a !== e.data.text && e.log(1);
            });
        },
        log: function(t) {
            console.log(t);
        },
        getDefaultConfig: function() {
            var t = this, e = a.colorMap, n = a.waterMarkConfig, o = e.filter(function(t) {
                return t[1] == n.color;
            });
            t.setData({
                ColorMap: e,
                defaultColorIndex: o.length ? o[0][0] : -1,
                defaultText: a.text,
                text: a.text
            });
        },
        onShareAppMessage: function(t) {
            return "button" === t.from && console.log(t.target), {
                title: "图片加水印-图片编辑助手",
                path: "pages/waterMarker/index",
                success: function() {},
                fail: function() {}
            };
        },
        more: function() {
            wx.navigateTo({
                url: "../more/index"
            });
        },
        go: function() {
            wx.navigateToMiniProgram({
                appId: "wx7c1b1fc80a4b43db",
                path: "pages/index/index",
                success: function() {}
            });
        },
        onLongTrap: function() {
            var t = this;
            wx.showActionSheet({
                itemList: [ "预览", "保存" ],
                success: function(a) {
                    console.log(a.tapIndex), 0 == a.tapIndex && t.previewImg(), 1 == a.tapIndex && t.saveImg();
                },
                fail: function(t) {
                    console.log(t.errMsg);
                }
            });
        }
    });
}();